﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ModBot.ConfigBuilder.Providers
{
    public class SpellDatabaseProvider
    {

        public List<Spell> Spells {get;set;}

        public SpellDatabaseProvider() 
        {
            Spells = new List<Spell>();            
        }


        public static SpellDatabaseProvider ParseSpellsFromFile(string filepath)
        {
            if(!File.Exists(filepath)) throw new Exception("Error spell file not found. Please ensure you have selected a valid file for parsing.");

            SpellDatabaseProvider provider = new SpellDatabaseProvider();

            try
            {
                FileInfo fileInfo = new FileInfo(filepath);
                string directory = fileInfo.DirectoryName;
                string fileName = fileInfo.Name;
                string connectionString = SharedLib.TextDriverFormatText;
                connectionString = String.Format(connectionString, directory + "\\");

                using (System.Data.Odbc.OdbcConnection connection = new System.Data.Odbc.OdbcConnection(connectionString))
                {
                    connection.Open();
                    using (System.Data.Odbc.OdbcCommand command = new System.Data.Odbc.OdbcCommand("select * from [" + fileName + "] where classes <> 'None'", connection))
                    {
                        using (System.Data.Odbc.OdbcDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                Spell spell = new Spell(Convert.ToInt32(reader["id"]), reader["name"].ToString());
                                provider.Spells.Add(spell);
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return provider;

        }
    }

    public class Spell
    {
        private int Id { get; set; }
        private string Name {get;set;}

        // ToDo : fully implement spell class

        public Spell(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }

        public override string ToString()
        {
            return this.Name;
        }
                    
    }
}
